/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shanhesoft.java.common.core.util.DateUtil;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.persistence.datascope.DataScope;
import com.shanhesoft.java.common.persistence.service.impl.DataServiceImpl;
import com.shanhesoft.java.modules.exam.domain.ExaminationRecord;
import com.shanhesoft.java.modules.exam.domain.PaperQue;
import com.shanhesoft.java.modules.exam.domain.dto.ExaminationRecordDto;
import com.shanhesoft.java.modules.exam.domain.dto.ExaminationRecordQueryCriteria;
import com.shanhesoft.java.modules.exam.domain.vo.ExaminationRecordVo;
import com.shanhesoft.java.modules.exam.repository.ExaminationRecordRepository;
import com.shanhesoft.java.modules.exam.service.ExaminationRecordService;
import com.shanhesoft.java.modules.exam.service.PaperQueService;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * 考试记录管理ServiceImpl 考试记录管理
 * @author admin
 * @version 2020-10-10 17:00:18
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ExaminationRecordServiceImpl extends DataServiceImpl<ExaminationRecordRepository, ExaminationRecord, ExaminationRecordDto, String> implements ExaminationRecordService{

	private final PaperQueService paperQueService;

	@Override
	public void saveOrUpdate(ExaminationRecordDto examinationRecordDto) {
		super.saveOrUpdate(examinationRecordDto);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public ExaminationRecordDto saveOrUpdateReturn(ExaminationRecordDto examinationRecordDto) {
		examinationRecordDto.setStartTime(new Date());
		examinationRecordDto.setSubmitStatus(0); // 0: '待批改', 1: '已批改', 2: '待批改', 3: '统计完成'
		examinationRecordDto.setScore(0L);
		examinationRecordDto.setCorrectNumber(0L);

		QueryWrapper<PaperQue> paperQueQueryWrapper = new QueryWrapper<PaperQue>();
		paperQueQueryWrapper.eq("paper_id", examinationRecordDto.getPaperId());
		int queCount = paperQueService.count(paperQueQueryWrapper);
		examinationRecordDto.setIncorrectNumber(Long.parseLong(""+queCount));

		super.saveOrUpdate(examinationRecordDto);

		return examinationRecordDto;
	}

	/**
	 * 分页查询考试记录
	 *
	 * @param pm 分页对象
	 * @return
	 */
	@Override
	public IPage<ExaminationRecordVo> findPage(PageModel pm, ExaminationRecordQueryCriteria examinationRecordQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, examinationRecordQueryCriteria);
		if(StringUtils.isNotEmpty(examinationRecordQueryCriteria.getUserId())) {
			wrapper.eq("a.user_id", examinationRecordQueryCriteria.getUserId());
		}

		IPage<ExaminationRecordVo> paperVoIPage = repository.findExamRecordVoPage(pm, wrapper);
		return paperVoIPage;
	}

	/**
	 * 查询考试记录对象
	 * @param id
	 * @return
	 */
	@Override
	public ExaminationRecordVo findExamRecordVobyId(String id) {
		ExaminationRecordVo examRecordVo = baseMapper.findExamRecordVobyId(id);
		if(null != examRecordVo && null != examRecordVo.getStartTime() && null != examRecordVo.getEndTime()) {
			examRecordVo.setDuration(getExamDuration(examRecordVo.getStartTime(), examRecordVo.getEndTime()));
		}

		return examRecordVo;
	}

	/**
	 * 查询考试记录对象
	 * @return
	 */
	@Override
	public IPage<Map<String, Object>> getWrongRecordsByUserId(PageModel pm, ExaminationRecordQueryCriteria examinationRecordQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, examinationRecordQueryCriteria);
		if(StringUtils.isNotEmpty(examinationRecordQueryCriteria.getUserId())) {
			wrapper.eq("b.user_id", examinationRecordQueryCriteria.getUserId());
		}
		IPage<Map<String, Object>> map = repository.getWrongRecordsByUserId(pm, wrapper);

		return map;
	}

	/**
	 * 计算持续时间
	 * @param startTime startTime
	 * @param endTime endTime
	 * @return String
	 */
	public static String getExamDuration(Date startTime, Date endTime) {
		// 持续时间
		String suffix = "分钟";
		Integer duration = DateUtil.getBetweenMinutes(startTime, endTime);
		if (duration <= 0) {
			duration = DateUtil.getBetweenSecond(startTime, endTime);
			suffix = "秒";
		}
		return duration + suffix;
	}
}
